package com.shopping.mall.core.annotation.service;


import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.responseStatus.mapper.ResponseStatusMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


/**
 * 系统日志：切面处理类
 */
//@Aspect
//@Component
public class LanguageAspect {
    @Autowired
    private Logger logger;
    @Autowired
    private ResponseStatusMapper responseStatusMapper;


    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation( io.swagger.annotations.ApiOperation )")
    public void logPointCut() {
    }

    //切面 配置通知
    @AfterReturning(pointcut = "logPointCut()", returning = "returning")
    public Object saveSysLog(JoinPoint joinPoint, Object returning) {
        try {

            //获取request
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            //从获取RequestAttributes中获取HttpServletRequest的信息
            assert requestAttributes != null;
            HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);

            String languageId=request.getHeader("languageId");
            System.out.println(languageId);

            //获取返回结果
            Result<Object> result = (Result<Object>) returning;

            String message=responseStatusMapper.getMessageLan(languageId,result.getStatus());
            if(message==null){
                message=responseStatusMapper.getMessage(result.getStatus());
            }
            result.setMessage(message);
            result.setLanguageId(languageId);
            return result;
        } catch (Exception e) {
            logger.error("语言转换失败");
            e.printStackTrace();
            return returning;
        }
    }

}
